//
//  AppDelegate.m
//  WisdomZhuHai
//
//  Created by yewei on 13-8-9.
//  Copyright (c) 2013年 wyd. All rights reserved.
//

#import "AppDelegate.h"
#import "Reachability.h"
#import "RootViewController.h"
#import "UMSocial.h"
#import "PaymentManager.h"
#import "Common.h"
#import "ProtocolManager.h"
#import "TransforController.h"
#import "MobClick.h"

@implementation AppDelegate

@synthesize shouldAutoRotation;
@synthesize hostReach;
@synthesize curNetworkStatus;
@synthesize logintime;

#define UM_KEY @"54574621fd98c55970005aec"
#define BAIDU_MAP_KEY @"WDFwV0sGLV9uRedIzzP2GlDe"


- (void)dealloc
{
    [_window release];
    [_viewController release];
    [hostReach release];
    [super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkChanged:) name: kReachabilityChangedNotification object: nil];
    self.hostReach = [Reachability reachabilityForInternetConnection];
    [hostReach startNotifier];
    curNetworkStatus = hostReach.currentReachabilityStatus;
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    self.shouldAutoRotation = NO;
    self.viewController = [[[RootViewController alloc] initWithNibName:nil bundle:nil ] autorelease];
//    if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
//    {
//        // warning: addSubView doesn't work on iOS6
//        [self.window addSubview:self.viewController.view];
//    }
//    else
//    {
//        // use this method on ios6
//        [self.window setRootViewController:self.viewController];
//    }
    [self.window addSubview:self.viewController.view];
    _mapManager = [[BMKMapManager alloc] init];
    BOOL ret = [_mapManager start:BAIDU_MAP_KEY generalDelegate:self];
    if (!ret) {
        NSLog(@"manager start failed!")
    }
    [_mapManager release];
    
    [self.window makeKeyAndVisible];
    [UMSocialData setAppKey:UM_KEY];
    [MobClick startWithAppkey:UM_KEY reportPolicy:BATCH   channelId:nil];
    
    NSString* temp = [[ProtocolManager shareInstance] getTimeStamp];
    self.logintime = temp;
    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    [MobClick setAppVersion:version];
    [MobClick setBackgroundTaskEnabled:YES];
    [MobClick setCrashReportEnabled:YES];
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    
    NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
    //app版本
    NSString* versionNum =[infoDict objectForKey:@"CFBundleVersion"];
    //手机型号
    NSString* phoneModel = [[UIDevice currentDevice] model];
    //手机系统版本
    NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];
    //物理地址
    //NSString* macAddr = [Common getMacAddr];
    NSString *phoneUUID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    
    NSString* loginname = [ProtocolManager shareInstance].loginName;
    if (loginname == nil) {
        loginname = @"匿名用户";
        [[ProtocolManager shareInstance] login:loginname password:self.logintime mobileModel:phoneModel loginCity:@"珠海" appVersion:versionNum OSversion:phoneVersion mac:phoneUUID target:self selector:nil];
        //记录匿名用户打开软件的时间
    } else {
        [[ProtocolManager shareInstance] logout:self selector:nil];
    }
}

- (void)networkChanged:(NSNotification*)note{
    Reachability* curReach= [note object];
    if ([curReach isKindOfClass:[Reachability class]]) {
        curNetworkStatus = [curReach currentReachabilityStatus];
    }
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    [[PaymentManager shareInstance] parse:url];
    return [UMSocialSnsService handleOpenURL:url];
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    [[PaymentManager shareInstance] parse:url];
    return  [UMSocialSnsService handleOpenURL:url];
}

//- (void)getWeatherFromApi {
//    [[ProtocolManager shareInstance]getWeather:self selector:@selector(getWeatherCallBack:)];
//}
//
//- (void)getWeatherCallBack:(NSDictionary *)dic {
//    NSLog(@"%@", dic);
//}


//-(BOOL)shouldAutorotate
//{
//    return YES;
//}
//
//-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
//{
//    if (self.shouldAutoRotation) {
//        return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
//    }
//    return UIInterfaceOrientationMaskPortrait;
//}

- (void)onGetNetworkState:(int)iError
{
    if (0 == iError) {
        NSLog(@"%s || 联网成功", __FUNCTION__);
    }
    else{
        NSLog(@"%s || onGetNetworkState %d",__FUNCTION__, iError);
    }
    
}

- (void)onGetPermissionState:(int)iError
{
    if (0 == iError) {
        NSLog(@" %s || 授权成功", __FUNCTION__);
    }
    else {
        NSLog(@"%s || onGetPermissionState %d",__FUNCTION__, iError);
    }
}

@end
